package src

import (
	"strings"

	zd "gitee.com/haifengat/zorm-dm"
	"github.com/casbin/casbin/v2/model"
	"github.com/casbin/casbin/v2/persist"
)

type AdapterDM struct {
	persist.Adapter
}

func (AdapterDM) AddPolicy(sec string, ptype string, rule []string) error {
	// 添加策略
	// policy := CasbinPolicy{
	// 	PType: ptype,
	// }
	// l := len(rule)
	// if l > 0 {
	// 	policy.V0 = rule[0]
	// }
	// if l > 1 {
	// 	policy.V1 = rule[1]
	// }
	// if l > 2 {
	// 	policy.V2 = rule[2]
	// }
	// if l > 3 {
	// 	policy.V3 = rule[3]
	// }
	// if l > 4 {
	// 	policy.V4 = rule[4]
	// }
	// if l > 5 {
	// 	policy.V5 = rule[5]
	// }
	// _, err := zd.Insert(ctxDao, false, policy)
	// return err
	return nil
}
func (AdapterDM) LoadPolicy(model model.Model) error {
	lines, err := zd.Select[CasbinPolicy](ctxDao, nil, nil)
	if err != nil {
		return err
	}

	for _, line := range lines {
		var p = []string{line.PType, line.V0, line.V1, line.V2, line.V3, line.V4, line.V5}
		var lineText string
		if line.V5 != "" {
			lineText = strings.Join(p, ", ")
		} else if line.V4 != "" {
			lineText = strings.Join(p[:6], ", ")
		} else if line.V3 != "" {
			lineText = strings.Join(p[:5], ", ")
		} else if line.V2 != "" {
			lineText = strings.Join(p[:4], ", ")
		} else if line.V1 != "" {
			lineText = strings.Join(p[:3], ", ")
		} else if line.V0 != "" {
			lineText = strings.Join(p[:2], ", ")
		}
		err := persist.LoadPolicyLine(lineText, model)
		if err != nil {
			return err
		}
	}
	return nil
}

func (AdapterDM) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
	return nil
}
func (AdapterDM) RemovePolicy(sec string, ptype string, rule []string) error {
	return nil
}
func (AdapterDM) SavePolicy(model model.Model) error {
	return nil
}
